package com.cx.mall.goods.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.cx.mall.common.model.goods.Category;
import com.cx.mall.goods.mapper.CategoryMapper;
import com.cx.mall.common.model.goods.CategoryDTO;
import com.cx.mall.goods.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品类目 服务实现类
 * </p>
 *
 * @author lyt
 * @since 2025-07-10
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {


    private final CategoryMapper categoryMapper;

    public CategoryServiceImpl(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    @Override
    public List<CategoryDTO> listAll() {
        //1、查询一级类别 parentid为null
        //1.1、构建条件构造器
        LambdaQueryChainWrapper<Category> oneWrapper = new LambdaQueryChainWrapper<>(baseMapper);
        //1.2、填充条件
        List<Category> ones = oneWrapper.isNull(Category::getParentId).list();
        //2、查询二级类别 parentid为一级类别中的id
        //2.1、构建条件构造器
        LambdaQueryChainWrapper<Category> twoWrapper = new LambdaQueryChainWrapper<>(baseMapper);
        //2.2、填充条件
        //2.2.1、获取一级类别的id
        List<Integer> oneIds = ones.stream().map(Category::getId).toList();
        //2.2.2、填充条件
        List<Category> twos = twoWrapper.in(Category::getParentId, oneIds).list();
        //查询三级类别
        //3.1、构建条件构造器
        LambdaQueryChainWrapper<Category> threeWrapper = new LambdaQueryChainWrapper<>(baseMapper);
        //3.2.1、获取二级类别的id
        List<Integer> twoIds = twos.stream().map(Category::getId).toList();
        //3.2.2、填充条件
        List<Category> threes = threeWrapper.in(Category::getParentId, twoIds).list();
        //4、组装数据
        //4.1、将三级类别组装到二级类别下
        //4.1.1、将三级类别转换成DTO
        List<CategoryDTO> threeDtos = JSON.parseArray(JSON.toJSONString(threes), CategoryDTO.class);
        //4.1.2将二级类别转换成DTO
        List<CategoryDTO> twoDtos =
                JSON.parseArray(JSON.toJSONString(twos), CategoryDTO.class);
        //4.1.3、填充(筛选二级类别下的三级类别）
        List<CategoryDTO> twoItems = twoDtos.stream().map(two -> {
            List<CategoryDTO> collect = threeDtos.stream().filter(three -> three.getParentId().equals(two.getId()))
                    .collect(Collectors.toList());
            two.setCategoryList(collect);
            return two;
        }).collect(Collectors.toList());
        //4.2 将二级类别组装一级类别下
        //4.2.1将一级类被转换成DTO
        List<CategoryDTO> oneDtos =
                JSON.parseArray(JSON.toJSONString(ones), CategoryDTO.class);
        //4.4.2 填充（筛选一级下的二级类别）
        List<CategoryDTO> list = oneDtos.stream().map(one -> {
            List<CategoryDTO> collect = twoItems.stream().filter(two -> two.getParentId().equals(one.getId()))
                    .collect(Collectors.toList());
            one.setCategoryList(collect);
            return one;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<Integer> listByParentId(List<Integer> ids) {
        LambdaQueryChainWrapper<Category> wrapper = new LambdaQueryChainWrapper<>(baseMapper);
        List<Integer> categoryIds = wrapper.in(Category::getParentId, ids)
                .select(Category::getId)
                .list()
                .stream()
                .map(Category::getId)
                .collect(Collectors.toList());
        return categoryIds;
    }

    @Override
    public List<Integer> listByParentId(Integer id) {
        LambdaQueryChainWrapper<Category> wrapper = new LambdaQueryChainWrapper<>(baseMapper);
        List<Integer> categoryIds = wrapper.eq(Category::getParentId, id)
                .select(Category::getId)
                .list()
                .stream()
                .map(Category::getId)
                .collect(Collectors.toList());
        return categoryIds;
    }

    /**
     * 获取spu类别
     */
    public List<String> getCategoryNames(Long categoryId){
        List<String> names = new ArrayList<>();
        // 获取三级类别名称
        Category three = categoryMapper.selectById(categoryId);
        if(three != null){
            names.add(three.getName());
            // 获取二级类别名称
            Category two = categoryMapper.selectById(three.getParentId());
            if(two != null){
                names.add(0,two.getName());
                // 获取一级类别名称
                Category one = categoryMapper.selectById(two.getParentId());
                if(one != null){
                    names.add(0,one.getName());
                }
            }
        }
        return names;
    }
}
